import { Component } from '@angular/core';
import {  NavController, NavParams, ViewController, LoadingController, ToastController } from 'ionic-angular';
import { BaseUI } from '../../common/baseui';
import { RestProvider } from '../../providers/rest/rest';

@Component({
  selector: 'page-register',
  templateUrl: 'register.html',
})
export class RegisterPage extends BaseUI {
  mobile: string;
  nickname: string;
  password: string;
  confirmPassword: string;
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public viewCtrl: ViewController,
    private LoadingCtrl: LoadingController,
    private toastCtrl: ToastController,
    private rest: RestProvider) {
    super();
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad RegisterPage');
  }
  dismiss() {
    this.viewCtrl.dismiss();
  }
  register() {
    //前台验证
    if (this.mobile == "") {
      super.showToast(this.toastCtrl, "手机号不能为空！")
    } else if (!(/^1[34578]\d{9}$/).test(this.mobile)) {
      super.showToast(this.toastCtrl, "您输入的手机号码格式不正确！")
    }else if (this.nickname == "") {
      super.showToast(this.toastCtrl, "昵称不能为空！")
    } else if (!(/^[A-Za-z_][A-Za-z_0-9]{2,9}$/).test(this.nickname)) {
      super.showToast(this.toastCtrl, "昵称以字母、数字或下划线组成，不能以数字开头,长度在 3~ 10 位之间。")
    } else if (this.password == "") {
      super.showToast(this.toastCtrl, "密码不能为空！")
    } else if (!(/^[a-zA-Z0-9]{6,12}$/).test(this.password)) {
      super.showToast(this.toastCtrl, "密码必须由数字字母组合，切长度在 6 ~ 12 位之间。")
    } else if (this.confirmPassword == "") {
      super.showToast(this.toastCtrl, "确认密码不能为空！")
    } else if (this.password != this.confirmPassword) {
      super.showToast(this.toastCtrl, "两次输入的密码不一致。")
    }  else {
      var loading = super.showLoading(this.LoadingCtrl, '注册中...');
      this.rest.register(this.mobile, this.nickname, this.password)
        .subscribe(data => {
          if (data["Status"] == "OK") {
            loading.dismiss();
            super.showToast(this.toastCtrl, "注册成功");
            this.dismiss();
          } else {
            loading.dismiss();
            super.showToast(this.toastCtrl, data["StatusContent"]);
          }
        });
    }
  }
  gLoginPage() {
    this.navCtrl.pop();
  }

}
